﻿Option Strict On
Option Explicit On

Imports LogginsLogDSTableAdapters

Partial Class Account_Login1
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString("ReturnUrl"))
        If Not Page.IsPostBack Then
            If Not Session("UserName1") Is Nothing Then
                Session.Remove("UserName1")
                'Session("UserName1")
            End If
            'Else
            '    Response.Write(Login1.UserName)
            '    If Not Session("UserName1") Is Nothing Then
            '        'Response.Redirect("~/private/default.aspx")
            '        Response.Write(Session("UserName1").ToString())
            '    End If
            If Not Request.QueryString("AccountDisabled") Is Nothing Then
                If Request.QueryString("AccountDisabled").ToString() = "True" Then

                    'Response.Write("Disabled")
                    DisabledByAdminModalPopupExtender.Show()
                End If
            End If

        End If
        'If Not Session("UserName1") Is Nothing Then
        '    'Response.Redirect("~/private/default.aspx")
        '    Response.Write(Session("UserName1").ToString() + " Cool")
        '    Response.Write(Session(Login1.UserName))
        'End If
        'Response.Write(Session("UserName1").ToString())
        'This is a time-based example
        Image1.Visible = False
        Dim intMinute As Int16 = CShort(Now.Minute)
        If intMinute >= 1 And intMinute < 20 Then
            Image1.ImageUrl = "~/i/iStock__3XSmall.jpg"
        ElseIf intMinute >= 20 And intMinute < 40 Then
            Image1.ImageUrl = "~/i/iStock__2XSmall.jpg"
        ElseIf intMinute >= 40 And intMinute < 59 Then
            Image1.ImageUrl = "~/i/iStock__1XSmall.jpg"
        End If
        Image1.Visible = True

    End Sub

    'Protected Sub Login1_Authenticate(sender As Object, e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

    '    'NENAD: I've added this code below for later
    '    'Ned sead: No, I have to catch Login controls error but if authenticate is handled there is no error
    '    'If user didn't confirm his email user cant log in, membership provider returns false, IsApproved in membership provider is false so user cant log in at all. 
    '    'This has to be handled in error event so when control(membership provider) returns error (false) 
    '    'because users email is not verified then check user and if exist (means that email is not verified) then send him wherever ... 
    '    'So if email is not confirmed user cant log in (in code or in the control) but when login control returns error you can do what ever ...        
    '    'Dim loginUsername As String = Login1.UserName
    '    'Dim loginPassword As String = Login1.Password
    '    'Dim IPAddress As String = Request.UserHostAddress

    '    'u = Membership.GetUser(Login1.UserName)
    '    ''Next, determine if the user's username/password are valid
    '    'If Membership.ValidateUser(loginUsername, loginPassword) Then
    '    '    If u.IsApproved Then
    '    '        e.Authenticated = True
    '    '    Else
    '    '        Response.Redirect("~/AccountEmailNotConfirmed.aspx")
    '    '    End If

    '    'Else
    '    '    e.Authenticated = False
    '    '    Login1.FailureText = "Your UserName/Password combination are invalid."
    '    'End If
    'End Sub

    Protected Sub Login1_LoggingIn(sender As Object, e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles Login1.LoggingIn
        Dim logginslogTA As New LogginsLogDSTableAdapters.SPTableAdapter()

        'Page.Validate()
        Try
            If Page.IsValid Then
                lblResult.Text = "+"
                lblResult.ForeColor = Drawing.Color.Green
            Else
                lblResult.Text = "-"
                lblResult.ForeColor = Drawing.Color.Red
                e.Cancel = True
            End If
        Catch ex As Exception
            Throw ex
        End Try

        'u = Membership.GetUser(Login1.UserName)
        'If Not u Is Nothing Then
        '    If Not u.IsApproved Then
        '        Session("UserName1") = u.UserName
        '    End If
        'End If

    End Sub

    Protected Sub Login1_LoginError(sender As Object, e As System.EventArgs) Handles Login1.LoginError
        u = Membership.GetUser(Login1.UserName)
        If Not u Is Nothing Then
            If Not u.IsApproved Then
                'Session("UserName1") = u.UserName
                'Response.Redirect("~/private/default.aspx")
                pnlEmailNotVerified_ModalPopupExtender.Show()

                'Response.Redirect("~/AccountEmailNotConfirmed.aspx")
                'Label1.Text = "Your email address is not yet confirmed.</br>Please log in your email account and click on link to confirm."
            End If
        Else
            Login1.FailureText = "Your UserName/Password combination are invalid."
        End If
        Try
            Dim logginslogTA As New LogginsLogDSTableAdapters.SPTableAdapter()
            logginslogTA.usp_InsertLogginsLog(Login1.UserName, Login1.Password, False, DateTime.Now)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub


    Protected Sub Login1_LoggedIn(sender As Object, e As System.EventArgs) Handles Login1.LoggedIn

        Dim logginslogTA As New LogginsLogDSTableAdapters.SPTableAdapter()
        Try
            logginslogTA.usp_InsertLogginsLog(Login1.UserName, Login1.Password, True, DateTime.Now)
        Catch ex As Exception
            Throw ex
        End Try

        u = Membership.GetUser(Login1.UserName)
        Dim usercheckTA As New usp_UserCheckTableAdapter()
        Dim usercheckDT As LogginsLogDS.usp_UserCheckDataTable

        Try
            usercheckDT = usercheckTA.GetData(CType(u.ProviderUserKey, Global.System.Guid?))
        Catch ex As Exception
            Throw ex
        End Try

        For Each usercheckRow As LogginsLogDS.usp_UserCheckRow In usercheckDT
            If Not usercheckRow.ApprovalRequired Then
                FormsAuthentication.SignOut()
                HttpContext.Current.Response.Redirect("~/Account/Login.aspx?AccountDisabled=True")
            End If
        Next

    End Sub

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        Response.Redirect("~/contactus.aspx")
    End Sub


End Class
